<?php
	include_once('../sys/sys.php');
	
	/**
	 * 用于根据ID生成查询字符串
	 * @param $strid	ID标识
	 */
	function make_init_str($strid,$page=0){
		return "strid=" . $strid . "&strpage=" . $page;
	}
	
	/**
	 * 根据服务器返回值生成查询散列
	 * @param $hash	服务器返回的哈希值
	 */
	function make_sub_str($hash){
		$hash = str_replace(array('[',']',"'",'null,'),'',$hash);
		$hash = trim($hash);
		$hash = explode(',',$hash);
		$hash[2] = $hash[3];
		unset($hash[3]);
		foreach ($hash as $k=>$v) {
			$hash[$k] = urlencode($v);
		}
		$submitstr = 'date1=' . $hash[0] . '&date2=' . $hash[1] . '&key=' . $hash[2];
		return $submitstr;
	}
	
	function make_wget_str($svpath,$cookiepath,$host,$postdata,$questpath){
		$st = 'wget -NP ' . $svpath . ' --load-cookies=' . $cookiepath .' --post-data="' . $postdata . '" --user-agent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Maxthon 2.0)"' ;
		$st .= ' "http://' . $host . $questpath;
		return $st;
	}
	
	
	/**
	 * 使用WGET构造伪查询用于获取后台图片
	 * @param $id	图片ID
	 * @param $pg	当前页
	 */
	function get_picurl($id,$pg=0,$only_length=false){
		//WGET远程调用
		$svpath = 'E:\web\hf\dm5\data';
		$cookiepath = 'E:\web\hf\dm5\data\cookie.txt';
		$host = "cnc.dm5.com";
		$questpath1 = '/ajax/DM5Func.FuncAjax,DM5Func.ashx?_method=ImageDes&_session=no';
		$questpath2 = '/ajax/DM5Func.FuncAjax,DM5Func.ashx?_method=UnImageDesTwo&_session=no';
		$questpath3 = '/ajax/DM5Func.FuncAjax,DM5Func.ashx?_method=UnImageDes&_session=no';
		$session1path = 'E:\web\hf\dm5\data\DM5Func.FuncAjax,DM5Func.ashx@_method=ImageDes&_session=no';
		$session2path = 'E:\web\hf\dm5\data\DM5Func.FuncAjax,DM5Func.ashx@_method=UnImageDesTwo&_session=no';
		$session3path = 'E:\web\hf\dm5\data\DM5Func.FuncAjax,DM5Func.ashx@_method=UnImageDes&_session=no';
		$postdata = make_init_str($id,$pg);
		$wgetquery_str = make_wget_str($svpath,$cookiepath,$host,$postdata,$questpath1);
		exec($wgetquery_str);
		$hash = file_get_contents($session1path);
		$length = count(explode(',',$hash));
		$sub_str = make_sub_str($hash);
		$pic_url_str = make_wget_str($svpath,$cookiepath,$host,$sub_str,$questpath2);
		exec($pic_url_str);
		
		$pic_url_str = file_get_contents($session2path);
		$pic_url_str = str_replace(array('[',']',"'",'null,'),'',$pic_url_str);
		$pic_url_str = trim($pic_url_str);
		$pic_url_str = explode(',',$pic_url_str);
		$pic_url_str = $pic_url_str[0];
		if($only_length)
			return $length;
		else 	
			return $pic_url_str;
	}
	
	function cc($url){
        $svpath = 'E:\web\hf\dm5\data';
		$cookiepath = 'E:\web\hf\dm5\data\cookie.txt';
		$host = "cnc.dm5.com";
		$execv = "wget --load-cookies=" . $cookiepath . "  \"" .  $url  . "\" -O E:\\web\\hf\\dm5\\data\\tmp";
		//echo $execv;exit;
		exec($execv);
		//exec("del E:\\web\\hf\\dm5\\data\\tmp");
	}
	
	
?>